package com.cszjzx.member.dao;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.cszjzx.member.po.Member;

public class MemberlockDAOImpl implements MemberlockDAO{
	private JdbcHelper helper;
	public MemberlockDAOImpl() throws IOException {
		helper=new JdbcHelper();
	}

	@Override
	public void LockMember(String idcard) throws SQLException {
		String sql="insert into memberlock (idcard,islock) value(?,?)";
		Object []args={idcard,1};
		helper.executeUpdate(sql, args);
	}

	@Override
	public void delete(String idcard) throws SQLException {
		String sql ="delete from memberlock where idcard=?";
		Object []args={idcard};
		helper.executeUpdate(sql, args);
	}
	
	@Override
	public boolean IsLock(String idcard) throws  SQLException, IOException, ClassNotFoundException {
		String sql ="select islock from memberlock where idcard=?";
		Object []args={idcard};
		List list=helper.executeQuery(sql, args, new RowMapper() {
			
			@Override
			public Object mapRow(ResultSet rs) throws SQLException, ClassNotFoundException, IOException {
				 return rs.getInt("islock")>0?true:false;
				
			}
		});
		if(list.isEmpty()){
			return false;
		}
		else {
			return (boolean) list.get(0);
		}
		
	}

	@Override
	public List<String> findMemberAndIslockByMajorAndProcess(String major, String process) throws ClassNotFoundException, SQLException, IOException {
		String sql="select member.idcard from member,memberlock where major = ? and process = ? and member.idcard = memberlock.idcard";
		Object[]args={major, process};
		return helper.executeQuery(sql, args, new RowMapper() {
			@Override
			public Object mapRow(ResultSet rs) throws SQLException, ClassNotFoundException, IOException {
				String s;
				s=rs.getString("member.idcard");
				return s;
			}
		});
	}
	
	public static void main(String[] args){
		try {
			new MemberlockDAOImpl().LockMember("123");
		} catch (SQLException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
